import axios from '../axios';

import AgentServer from '@/modules/Agent';
import { sm2 } from 'sm-crypto';

export interface customerInvitationLinksProps {
	bizScenId: number;
	org: string;
	type: number;
	sessionId: string;
	customerId: string;
	customerRole: string;
	startDate: string;
	endDate: string;
}

export interface ICreatePoint {
	recordingId: string;
	content: string;
	timePoint: number;
	userId: number | string;
	userName: string;
	userRole: string;
}

/**
 * 获取访客提交的form信息
 * @param {*} formid
 */
const getuserforminfo = (formid: string | number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/formInfo/' + formid,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取所有启用的业务场景
 */
const getAllBizScenario = () => {
	// status = enabled 表示所有启用的业务场景  disabled 表示所有停用的业务场景
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/bizscenarios/select/options?status=enabled',
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 根据业务场景查询分组信息
 * @param id
 * @returns
 */
const getBizscenariosInfo = (id: number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/bizscenarios/${id}/details`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 根据坐席工号获取绑定的业务场景
 * @param {String} username 坐席工号
 * @param {String} token token
 */
const getAgentBindBizScenario = (username: string) => {
	return axios({
		url: `${AgentServer.configure.serviceUrl}/ivcs/api/v1/bizscenarios/agent/${username}`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取会话的所有记录
 * @param {*} sessionId
 */
const getsessiondetail = (sessionId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/${sessionId}/im-operation`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 上传文件
 * @param {*} data
 */
const uploadFileWithFormData = (data: any) => {
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/upload',
		data,
		method: 'post',
		headers: {
			'Content-Type': 'multipart/form-data',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 创建访客会话邀请码
 * @param data
 */
const customerInvitationLinks = (data: customerInvitationLinksProps) => {
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/customerInvitationLinks',
		data,
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 上传图片根据blob
 * @param blob
 * @param fileName
 * @returns
 */
const uploadFileWithBlob = (blob: string | Blob, fileName: string | undefined) => {
	let data = new FormData();
	data.append('file', blob, fileName);
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/upload',
		data,
		method: 'post',
		headers: {
			'Content-Type': 'multipart/form-data',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 人脸识别
 * @param userId 用户id
 * @param para 图片参数
 * @returns {Promise<AxiosResponse<T>>}
 */
const compareFace = (userId: string, data: any) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/users/` + userId + '/compare-face',
		data,
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 人证对比
 * @param certName 姓名
 * @param certNo 身份证号
 * @param faceContrastPicture 人脸图片BASE64编码
 * @param faceContrastPictureUrl 人脸图片URL
 * @returns
 */

const contrastverify = (data: { certName: string; certNo: string; faceContrastPicture?: string; faceContrastPictureUrl?: string }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/ai/matchFaceAndIdCard`,
		data,
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//根据坐席工号保存按钮顺序
const saveAgentProfile = (data: any[]) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/agentProfile`,
		data,
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//根据坐席工号获取按钮顺序
const getAgentProfile = () => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/agentProfile`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

// 获取表单列表
const getFormList = (bizscenariosId: string | number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/bizscenarios/${bizscenariosId}/forms`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

// 获取表单流水号
const getFormFollowId = (data: { org: string; fromClientId: string; toClientId: string; sessionId: string }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/formHistory`,
		method: 'post',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//更新表单记录
const updateFormRecord = (data: { id: string | number; status: string }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/formHistory/${data.id}`,
		method: 'put',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

// 分页获取指定分组下相关坐席
const findAgents = (data: any) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/agents/find`,
		method: 'post',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//判断当前用户是否是当前分组的班组长坐席
const judgeChief = (userId: number | string, groupId: number | string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/user-group-relations/isChief?userId=' + userId + '&groupId=' + groupId,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取roomtoken，用于通话管理中心，静默观战
 * @param {object} data room:roomid,
			user:userName?userName:'admin',
			role:'presenter'
 * @param {string} token
 */
const getRoomToken = (data: { room: string; user: string; role: string }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/tokens',
		method: 'post',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

// 检查录像打点
const checkRecordPoint = (recordingId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/private/recording/${recordingId}/videomarks/check-count`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

// 创建录像打点
const createRecordPoint = (data: ICreatePoint) => {
	const { content, timePoint } = data;
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/private/recording/${data.recordingId}/videomarks`,
		method: 'post',
		data: { content, timePoint },
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};
/**
 * 个人证照识别
 * @param url string 身份证url地址, url和base64str必须传一个，如果都传以url为准
 * @param base64str string 图片二进制文件 /9j/4QCCRXhpZgAATU0A.... |
 */
const idCardOcr = (url: string | null, base64str: string | null) => {
	const data = { url: url, base64str: base64str };
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/ali-ai/ocr/idCard',
		data,
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 根据流ID 移除数字人流
 * @param { String } room  房间号
 * @param { String } streamId 流 ID
 * @param { String } token
 * @returns
 */
const dropRobotStream = (room: string, streamId: string) => {
	return axios({
		url: `${AgentServer.configure.serviceUrl}/ivcs/api/v1/rooms/${room}/streams/${streamId}`,
		method: 'delete',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};
/**
 * 个银行卡识别
 * @param url string 银行卡url地址, url和base64str必须传一个，如果都传以url为准|
 * @param base64str string 图片二进制文件 /9j/4QCCRXhpZgAATU0A.... |
 */
const bankCardOcr = (url: string | null, base64str: string | null) => {
	const data = { url: url, base64str: base64str };
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/ali-ai/ocr/bankCard',
		data,
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//获取证言列表
const getTestimoneyList = (bizscenarioId: string | number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/testimony/list?bizscenarioId=${bizscenarioId}`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

// 获取变量列表
// scenId : 业务场景ID
// initMode : 初始化方式 user-data non-init
// isInvalid 是否作废 true  false
const getVariablesList = (data: { scenId: string | number; initMode: string; isInvalid: string; page: number; pageSize: number }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/bizscenarios/${data.scenId}/variables?page=${data.page}&pageSize=${data.pageSize}&initMode=${data.initMode}&isInvalid=${data.isInvalid}`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//获取业务场景的`人工坐席TTS辅助播报`开关
const getTTSOption = (bizscenarioId: string | number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/bizscenarios/${bizscenarioId}/agentTtsOption`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//启动人工坐席TTS通道
const enableTTS = (roomId: string | number, data: any) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/private/rooms/${roomId}/tts`,
		data,
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//停止人工坐席TTS通道
const stopTTS = (roomId: string | number, ttsId: string | number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/private/rooms/${roomId}/tts/${ttsId}`,
		method: 'delete',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//人工坐席TTS开始说
const startSpeaking = (roomId: string | number, ttsId: string | number, data: any) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/private/rooms/${roomId}/tts/${ttsId}/start`,
		data,
		method: 'patch',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//人工坐席TTS停止说
const stopSpeaking = (roomId: string | number, ttsId: string | number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/private/rooms/${roomId}/tts/${ttsId}/stop`,
		method: 'patch',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

// 获取当前会话当前场景的变量值集合
const getVariableWithSessionId = (sessionId: string, bizscenarioId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/${sessionId}/scenarios/${bizscenarioId}/variableValues `,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 保存会话form信息
 * @param {Sting} sessionId 会话id
 * @param {Sting} formId 表单id
 *
 */
const saveSessionForm = (data: { sessionId: string; formId: string }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/form `,
		data,
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 判断坐席是否绑定了业务场景对应的分组
 * @param agentName 坐席id
 * @param scenId 业务场景id
 * @returns
 */
const agentIsBoundBizscenarios = (agentName: string, scenId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/agents/names/${agentName}/bizscenarios/${scenId}/isBound `,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 判断坐席是否签入了业务场景对应的分组
 * @param agentName
 * @param scenId
 * @returns
 */
const agentIsCheckInBizscenarios = (agentName: string, scenId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/agents/names/${agentName}/bizscenarios/${scenId}/isCheckIn `,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 判断坐席工号是否存在
 * @param agentName
 * @returns
 */
const agentIdIsExistence = (agentName: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/auth/users/names/${agentName}/roles/agent`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/** 应用背景图片 */
export const saveBackgroundImage = (data: { backgroundId: number }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/background/userBackground`,
		method: 'put',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/** 新增背景图片 */
export const addBackgroundImage = (data: { url: string; abbreviatedUrl: string; fileName: string; isDefault: boolean; type: 'no' | 'normal' | 'fuzzy' }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/background/create`,
		method: 'post',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/** 获取背景图片 */
export const getBackgroundImageList = (page: number, pageSize: number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/background/list?page=${page}&pageSize=${pageSize}`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/** 删除背景图片 */
export const delBackgroundImage = (backgroundId: number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/background/${backgroundId}`,
		method: 'delete',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/** 获取小休状态 */
export const getOtherWorkStatus = () => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/agents/otherwork/config`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/** 保存小休状态 */
export const saveOtherWorkStatus = (data: { sessionOver: boolean; timeoutOver: boolean; reasonForSessionOver: string; reasonForTimeoutOver: string }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/agents/otherwork/config`,
		method: 'post',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

export const getCustomerAndBusinessVariables = (sessionId: string, scenarioId: string | number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/${sessionId}/scenarios/${scenarioId}/variableValues`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

export const getAllRoleWithBusiness = (scenarioId: string | number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/bizscenarios/${scenarioId}/roles`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//获取实时质检报告记录明细
export const getBizflowQualityRecord = (sessionId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/${sessionId}/bizflowquality/record`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//校验机器人坐席
export const getAgentNameIsRobot = (org: string, name: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/agents/org/${org}/name/${name}/isRobot`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

//获取会话类型
export const getSessionType = (sessionId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/${sessionId}/detail`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};
/**
 * 创建水印
 * @param data {object} { url: string; abbreviatedUrl: string; fileName: string; isDefault: boolean }
 * @returns { Promise }
 */
export const createWaterMark = (data: { url: string; abbreviatedUrl: string; fileName: string; isDefault: boolean }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/mark/create`,
		method: 'post',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取水印列表
 * @param params { page: number; pageSize: number }
 * @returns { Promise }
 */
export const getWaterMarkList = (params: { page: number; pageSize: number }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/mark/list`,
		params,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 更新水印
 * @param data  { markId: number }
 * @returns
 */
export const putWaterMark = (data: { markId: number }) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/mark/userMark`,
		data,
		method: 'put',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 删除水印
 * @param id {number}
 * @returns
 */
export const delWaterMark = (id: number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/mark/${id}`,
		method: 'delete',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

const getAllGroupSkill = () => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/groupSkill`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

interface TTSParams {
	scenId: string | number;
	page: number;
	pageSize: number;
}
/**
 * 分页获取 TTS 话术列表
 */
const fetchTTSTalks = (params: TTSParams) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/bizscenarios/${params.scenId}/ttsspeech?page=${params.page}&pageSize=${params.pageSize}`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

interface BusinessParams {
	scenId: string | number;
	type: 'document' | 'video';
	page: number;
	pageSize: number;
}
/**
 * 分页获取业务场景资料
 */
const fetchBusinessMaterial = (params: BusinessParams) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/bizscenarios/${params.scenId}/material?type=${params.type}&page=${params.page}&pageSize=${params.pageSize}`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

interface FetchAgentListParams {
	scenId: string | number;
	page: number;
	pageSize: number;
}
/**
 * 查询当前业务场景下，所有坐席状态
 * @param {FetchAgentListParams} scenId
 * @returns
 */
const fetchAgentListWithBizscenarioId = (scenId: string | number) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/bizscenarios/${scenId}/available/agent`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 系统设置列表数据
 */
export const getConfigList = () => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/systemconfig/list`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 更新会话是否接通状态
 * @param sessionId
 * @param audioVideoCallIsConnected： true, // true || false
 * @returns
 */
export const updateAudioVideoCallIsConnectedStatus = (sessionId: string, audioVideoCallIsConnected: boolean) => {
	const data = { audioVideoCallIsConnected: audioVideoCallIsConnected };
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/${sessionId}/audioVideoCallIsConnected`,
		method: 'put',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取人工工作流
 * @param {int} id 人工工作流ID
 * @returns
 */
export const getHumanflowById = (id: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/humanFlow/' + id,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取人工工作流执行步骤
 * @param sessionId
 * @returns
 */
export const getHumanflowSteps = (sessionId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/humanFlow/steps/sessions/${sessionId}`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 提交人工工作流执行步骤
 * @param data { humanStepId: number; content: string; sessionId: string; bizScenarioId: number }
 * @returns
 */
export const postHumanflowStep = (data: any) => {
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/humanFlow/step',
		method: 'post',
		data,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 知识库问答
 * @param {*} question
 * @param {*} categoryId
 * @returns
 */
export const getKnowledgeBaseAsk = (question: string, categoryId: string) => {
	let url = AgentServer.configure.serviceUrl + `/ivcs/api/v1/knowledge/ask?question=${question}`;
	if (categoryId && categoryId !== 'all') {
		url = url + `&categoryId=${categoryId}`;
	}
	return axios({
		url: url,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 问答类型知识库搜索问题
 * @param {*} question 问题
 * @param {*} categoryId 分类id
 * @param {*} id 知识库id
 * @returns
 */
export const getQaKnowledgeBaseAsk = (question: string, id: string, categoryId: string) => {
	let url = AgentServer.configure.serviceUrl + `/ivcs/api/v1/knowledgeV1/${id}/qaFile?page=1&pageSize=1000&name=${question}`;
	if (categoryId && categoryId !== 'all') {
		url = url + `&categoryId=${categoryId}`;
	}
	return axios({
		url: url,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取问答对类型知识库分类列表
 * @returns
 */
export const getKnowledgeBaseCategoryOptions = (id: any) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/knowledgeV1/${id}/category`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取问答类型知识库列表
 * @returns
 */
export const getQaKnowledgeBaseOptions = () => {
	return axios({
		url: AgentServer.configure.serviceUrl + '/ivcs/api/v1/knowledgeV1/qa/option',
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取会话小结
 * @param sessionId
 * @returns
 */
export const getSessionSummary = (sessionId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/axa/v1/sessions/${sessionId}/summary`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/* 获取会话记录列表
 * @param data { sessionId, acceptedStartTime, acceptedEndTime, isAccepted, isConnected, scenarioName, score, page, pageSize }
 * @returns
 */
export const getSessionRecordList = (data: any) => {
	const { sessionId, acceptedStartTime, acceptedEndTime, isAccepted, isConnected, scenarioName, score, page, pageSize } = data;
	const params: any = { sessionId, acceptedStartTime, acceptedEndTime, isAccepted, isConnected, scenarioName, score, page, pageSize };
	params.agentName = AgentServer.user?.userName || localStorage.getItem('agent_user_name');
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/serviceList`,
		method: 'get',
		params: params,
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取会话小结配置项列表
 * @param scenId
 * @returns
 */
export const getSessionSummaryList = (scenId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/bizscenarios/${scenId}/summary`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 根据sessionID获取已经提交的会话小结
 * @param sessionId
 * @returns
 */
export const getSessionBizSummary = (sessionId: string) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/${sessionId}/bizSummary`,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 会话小结确认
 * @param sessionId
 * @param data { scenId, bizSummary: [{bizSummaryId, name, type, data, isRequired, value }] }
 * @returns
 */
export const confirmSessionBizSummary = (sessionId: string, data: any) => {
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/${sessionId}/bizSummary/confirmation`,
		data,
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 获取是否有需要做小结的会话
 * @returns
 */
export const getUnfinishedSummaryStatus = () => {
	const params = { agentName: AgentServer.user?.userName || localStorage.getItem('agent_user_name') };
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/needDoSummary`,
		params: params,
		method: 'get',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};
/**
 * 更新更新用户信息
 * @param {*} userId
 * @param {*} data
 */
export const changeUserformInfo = (userId: string, data: any) => {
	let params = [...data];
	if (params.length > 0) {
		params.forEach(item => {
			if (item.op === 'replace' && item.path === '/pwd') {
				let password = item.value;
				if (publicKey) {
					// const sm2 = require('sm-crypto').sm2;
					const cipherMode = 1; // 1 - C1C3C2，0 - C1C2C3，默认为1
					let encryptData = sm2.doEncrypt(password, publicKey, cipherMode); // 加密结果
					password = encryptData;
				}
				item.value = password;
			}
		});
	}
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/auth/users/${userId}`,
		data: params,
		method: 'patch',
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		}
	});
};

/**
 * 服务记录导出
 * @param params
 * @returns
 */
export const exportSessionList = (params: {
	sessionId?: string;
	acceptedStartTime?: any;
	acceptedEndTime?: any;
	isAccepted?: boolean;
	isConnected?: boolean;
	scenarioName?: string;
	score?: number;
}) => {
	const agentName = AgentServer.user?.userName || localStorage.getItem('agent_user_name');
	return axios({
		url: AgentServer.configure.serviceUrl + `/ivcs/api/v1/sessions/serviceList/export`,
		method: 'get',
		params: { ...params, agentName },
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': AgentServer.getToken()
		},
		responseType: 'arraybuffer'
	});
};

export {
	getuserforminfo,
	getAllBizScenario,
	getAgentBindBizScenario,
	getsessiondetail,
	uploadFileWithFormData,
	customerInvitationLinks,
	uploadFileWithBlob,
	compareFace,
	contrastverify,
	saveAgentProfile,
	getAgentProfile,
	getFormList,
	getFormFollowId,
	updateFormRecord,
	findAgents,
	judgeChief,
	getRoomToken,
	checkRecordPoint,
	createRecordPoint,
	idCardOcr,
	bankCardOcr,
	dropRobotStream,
	getTestimoneyList,
	getVariablesList,
	getTTSOption,
	enableTTS,
	stopTTS,
	startSpeaking,
	stopSpeaking,
	getVariableWithSessionId,
	saveSessionForm,
	agentIsBoundBizscenarios,
	agentIsCheckInBizscenarios,
	agentIdIsExistence,
	getAllGroupSkill,
	getBizscenariosInfo,
	fetchTTSTalks,
	fetchBusinessMaterial,
	fetchAgentListWithBizscenarioId
};
